﻿using HBKY.Shop.Common.Helper;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web.LayoutRenderers;
using System.Diagnostics;
using System.Text;

namespace HBKY.Shop.Common.LogHelper
{
    /// <summary>
    /// 链路标识
    /// </summary>
    [LayoutRenderer("traceId")]
    [ThreadAgnostic]
    public class TraceIdLayoutRenderer : AspNetLayoutRendererBase
    {
        protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
        {
            // 记录请求上下文的标识符 数据库标识ActivityId
            builder.Append(Trace.CorrelationManager.ActivityId.ToString());
        }
    }

    [LayoutRenderer("clientip")]
    [ThreadAgnostic]
    public class ClientIPLayoutRenderer : AspNetLayoutRendererBase
    {
        protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
        {
            // 记录请求真实Ip
            builder.Append(base.HttpContextAccessor.HttpContext.GetClientIP());
        }
    }

    [LayoutRenderer("requesturl")]
    [ThreadAgnostic]
    public class RequestUrlLayoutRenderer : AspNetLayoutRendererBase
    {
        protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
        {
            //请求路径
            builder.Append(HttpContextAccessor.HttpContext.Request.Path.ToString() ?? string.Empty);
        }
    }

    //[LayoutRenderer("userid")]
    //[ThreadAgnostic]
    //public class UserIdLayoutRenderer : AspNetLayoutRendererBase
    //{
    //    protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
    //    {
    //        //请求路径
    //        builder.Append(base.HttpContextAccessor.HttpContext.GetUserId()?.ToString());
    //    }
    //}
}
